import router from "./router";
import {initMenu} from "./utils/menu";
import store from "./store";
import {getInfo} from '@/api/login'
import Vue from "vue";


router.beforeEach((async (to, from, next) => {
    console.log("to", to)
    console.log("from", from)
    console.log("next", next)
    console.log("router", router)
    if (window.sessionStorage.getItem('token')) {
        initMenu(router, store);
        if (!window.sessionStorage.getItem('loginInfo')) {
            //判断用户信息是否存在
            debugger
            await getInfo().then(data => {
                if (data) {
                    //存入用户信息
                    window.sessionStorage.setItem('loginInfo', JSON.stringify(data));
                    next();
                }
            })
        }
        next();
    } else {
        if (to.path == '/') {
            next();
        } else {
            next('/?redirect=' + to.path);
        }
    }
}))

/** 权限指令,对按钮权限的控制 **/
Vue.directive('has', {
    inserted: function (el, binding) {
        if (!permissionJudge(binding.value)) {
            el.parentNode.removeChild(el);
        }

        function permissionJudge(value) {
            // 此处store.getters.getMenuBtnList代表vuex中储存的按钮菜单数据
            var loginInfo = window.sessionStorage.getItem('loginInfo')
            var  urls = JSON.parse(loginInfo).urls
            if(urls.indexOf(value)!=-1){
                return true;
            }
            return false;
        }
    }
})
